﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BudgetSV.Common;
using BudgetData;

namespace BudgetSV.admin.manager
{
    public partial class role_list : ManagePage
    {
        protected string keywords = string.Empty;
        public Organization dptl;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("manager_role", DTEnums.ActionEnum.View.ToString()); //检查权限

                Manager model = GetAdminInfo(); //取得当前管理员信息
                dptl = GetOrgBymanager(model.id);
                RptBind(model.org_id);
            }
        }

        #region 数据绑定=================================

        private void RptBind(long orgid)
        {
            this.txtKeywords.Text = this.keywords;
            this.rptList.DataSource = Mod.GetList<ManagerRole>("select * from ManagerRole where org_id in (0,"+ orgid + ") and role_name like '%"+ keywords + "%'");
            this.rptList.DataBind();
        }
        public string Getlksr(object _id,object _nm,object isys) {
            if(isys.ToString()=="1")
            //if (_nm.ToString() == "超级管理员")
            {
                return "<span style=\"color:#888;\">" + _nm.ToString() + "</span>";
            }
            else {
                return "<a href=\"role_edit.aspx?action="+ DTEnums.ActionEnum.Edit + "&id="+_id.ToString()+"\">"+ _nm .ToString()+ "</a>";
            }
        }
        public string Geteditsr(object _id, object _nm, object isys)
        {
            if (isys.ToString() == "1")
            //if (_nm.ToString() == "超级管理员")
            {
                return "<span style=\"color:#888;\">修改</span>";
            }
            else {
                return  "<a href=\"role_edit.aspx?action=" + DTEnums.ActionEnum.Edit + "&id=" + _id.ToString() + "\">修改</a>";
            }
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and role_name like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回角色类型名称=========================
        protected string GetTypeName(int role_type)
        {
            string str = "";
            switch (role_type)
            {
                case 1:
                    str = "超级用户";
                    break;
                default:
                    str = "系统用户";
                    break;
            }
            return str;
        }
        #endregion
        //查询操作
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("role_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("manager_role", DTEnums.ActionEnum.Delete.ToString()); //检查权限

            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                long id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    ManagerRoleValue vm = new ManagerRoleValue();
                    Mod.Delete<ManagerRoleValue>(c => c.role_id == id);

                    if (Mod.Delete<ManagerRoleValue>(c => c.id == id)>0)
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除管理角色" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("role_list.aspx", "keywords={0}", this.keywords), "Success");
        }
    }
}